package cn.gzsxt.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.gzsxt.po.PageResult;
import cn.gzsxt.po.SysUser;
import cn.gzsxt.query.SysUserQueryObject;
import cn.gzsxt.service.SysUserService;
import cn.gzsxt.service.impl.SysUserServiceImpl;
import cn.gzsxt.vo.MsgObj;

@WebServlet("/sysUser.gzsxt")
public class UserHandlerController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 引入Service层
	private SysUserService service = new SysUserServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 设置响应编码和MIME类型
		resp.setContentType("application/json;charset=utf8");

		// 1.接受标识参数 cmd :用于判断不同的功能 checkUname , addUser ,
		// updateUser,deleteUser,userList
		String cmd = req.getParameter("cmd");
		if ("checkUname".equals(cmd)) {
			checkUname(req, resp);
		} else if ("addUser".equals(cmd)) {
			addUser(req, resp);
		} else if ("userList".equals(cmd)) {
			userList(req, resp);
		} else if ("deleteUser".equals(cmd)) {
			deleteUser(req, resp);
		} else if ("updateUser".equals(cmd)) {
			updateUser(req, resp);
		}
	}

	// 根据id修改指定用户的信息
	private void updateUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 1.调用请求参数封装对象的方法
		SysUser sysUser = this.getParameter2Obj(req);
		// 2.调用业务层修改方法
		int row = service.updateSysUser(sysUser);
		// 创建消息对象
		MsgObj msgObj = null;
		if (row == 1) {// 修改成功
			msgObj = new MsgObj(1, "修改成功");
		} else {
			msgObj = new MsgObj(0, "修改失败");
		}
		// 将处理结果响应给前台
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}

	// 根据用户id 删除指定的用户
	private void deleteUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 1.接受需要删除的用户id
		String id = req.getParameter("id");
		// 判断id是否为空
		if (id != null && !"".equals(id.trim())) {
			// TODO正则判断 Pattern类
			// 2.调用业务层的删除方法
			int row = service.deleteUser(Integer.valueOf(id));
			// 创建消息对象
			MsgObj msgObj = null;
			if (row == 1) {// 删除成功
				msgObj = new MsgObj(1, "删除成功");
			} else {
				msgObj = new MsgObj(0, "删除失败");
			}
			// 将处理结果响应给前台
			resp.getWriter().print(JSON.toJSONString(msgObj));
		}
	}

	// 根据条件 查询用户列表
	private void userList(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 1.接受各种条件参数: 关键字(条件搜索),页面,每页条数,封装成SysUserQueryObject 条件对象
		// 用户如果没有提交此参数:接受就是null
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		// 关键字
		String keyword = req.getParameter("keyword");
		// TODO正则判断 Pattern类
		SysUserQueryObject qo = new SysUserQueryObject();
		if (currentPage != null) {
			qo.setCurrentPage(Integer.valueOf(currentPage));
		}
		if (pageSize != null) {
			qo.setPageSize(Integer.valueOf(pageSize));
		}
		if (keyword != null && !"".equals(keyword.trim())) {
			qo.setKeyword(keyword);
		}
		// 2.调用service业务方法查询出对应的用户列表
		PageResult pageResult = service.list(qo);
		// 3.共享数据
		req.setAttribute("pageResult", pageResult);
		// 共享条件:关键字,方便前台会显,条件情况下进行翻页操作
		req.setAttribute("keyword", qo.getKeyword());
		// 4.使用请求转发跳转到 user_list.jsp
		req.getRequestDispatcher("/WEB-INF/sysuser/user_list.jsp").forward(req,
				resp);
	}

	// 处理检查用户是否存在操作
	private void checkUname(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 接受用户名称
		String uname = req.getParameter("uname");
		// TODO 使用正则进行校验
		// 调用业务层方法
		SysUser user = service.checkUname(uname);
		boolean flag = false;
		// 判断用户是否存在
		if (user == null) {
			// 不存在
			flag = true;
		}
		// 将json数据响应到浏览器
		resp.getWriter().print(flag);
	}

	// 添加用户操作
	private void addUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 调用请求参数封装对象的方法
		SysUser sysUser = this.getParameter2Obj(req);

		// 调用service的保存业务藏法
		int row = service.save(sysUser);
		System.out.println(row);
		// 创建消息对象
		MsgObj msgObj = null;
		if (row == 1) {
			msgObj = new MsgObj(1, "恭喜添加用户成功成功!!");
		} else {
			msgObj = new MsgObj(0, "服务器出现异常,正常修复bug中!!");
		}
		// 响应消息对象
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}

	public SysUser getParameter2Obj(HttpServletRequest req) {
		// 1.接受参数 账号,昵称,密码,邮箱,电话,qq,注册日期,封装成SysUser对象
		String uname = req.getParameter("uname");
		String pwd = req.getParameter("pwd");
		String nickname = req.getParameter("nickname");
		String email = req.getParameter("email");
		String phone = req.getParameter("phone");
		String qq = req.getParameter("qq");
		String regtime = req.getParameter("regtime");
		String id = req.getParameter("id");
		// TODO 正则判断 Pattern常用类(自学)

		SysUser sysUser = new SysUser();
		sysUser.setUname(uname);
		sysUser.setPwd(pwd);
		sysUser.setNickname(nickname);
		sysUser.setEmail(email);
		sysUser.setPhone(phone);
		sysUser.setQq(qq);
		if (id != null && !"".equals(id.trim())) {
			sysUser.setId(Integer.valueOf(id));
		}
		try {
			// 格式化日期
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = format.parse(regtime);
			sysUser.setRegtime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sysUser;
	}
}
